package com.yunfei.service.impl;

import com.yunfei.api.user.contants.ApiValitationConstantMessage;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.contants.ValitationConstantMessage;
import com.yunfei.enums.AdminLoginResultStatus;
import com.yunfei.enums.ResultStatus;
import com.yunfei.service.ValidMobileAndCodeService;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.List;

/**
 * @program: zntg_v1
 * @description:
 * @author: zhengyunfei
 * @create: 2018-10-30 15:48
 **/
@Service("validMobileAndCodeService")
public class ValidMobileAndCodeServiceImpl implements ValidMobileAndCodeService {
    /***校验手机号码*/
    public boolean ValidMobile(BindingResult bindingResult, ResultModel resultModel) {
        boolean mobileIsNull=false;
        boolean mobileIsReg=false;
        List<ObjectError> list = bindingResult.getAllErrors();
        for (ObjectError error : list) {
            String defaultMessage=error.getDefaultMessage();
            if(defaultMessage.equals(ApiValitationConstantMessage.MOBILE_NOTNULL_MESSAGE)){
                mobileIsNull=true;
            }
            else if(defaultMessage.equals(ApiValitationConstantMessage.MOBILE_REG_MESSAGE)){
                mobileIsReg=true;
            }
            else if(defaultMessage.equals(ApiValitationConstantMessage.MOBILE_REG_MESSAGE)){
                mobileIsReg=true;
            }
        }
        if(mobileIsNull){
            resultModel.setMessage(ResultStatus.MOBILE_ISNOTNULL.getMessage());
            resultModel.setCode(ResultStatus.MOBILE_ISNOTNULL.getCode());
            return true;
        }
        if(mobileIsReg){
            resultModel.setMessage(ResultStatus.MOBEILE_ERROR.getMessage());
            resultModel.setCode(ResultStatus.MOBEILE_ERROR.getCode());
            return true;
        }
        return false;
    }
    /***校验token*/
    public boolean ValidToken(BindingResult bindingResult, ResultModel resultModel) {
        boolean tokenIsNull=false;
        List<ObjectError> list = bindingResult.getAllErrors();
        for (ObjectError error : list) {
            String defaultMessage=error.getDefaultMessage();
            if(defaultMessage.equals(ApiValitationConstantMessage.TOKEN_ISNOTNULL_MESSAGE)){
                tokenIsNull=true;
            }

        }
        if(tokenIsNull){
            resultModel.setMessage(ResultStatus.TOKEN_NOTNULL.getMessage());
            resultModel.setCode(ResultStatus.TOKEN_NOTNULL.getCode());
            return true;
        }

        return false;
    }

    /***校验手机号码和验证码*/
    public boolean ValidMobileAndCode(BindingResult bindingResult, ResultModel resultModel) {
        boolean mobileIsNull=false;
        boolean mobileIsReg=false;
        boolean codeIsNull=false;
        List<ObjectError> list = bindingResult.getAllErrors();
        for (ObjectError error : list) {
            String defaultMessage=error.getDefaultMessage();
            if(defaultMessage.equals(ApiValitationConstantMessage.MOBILE_NOTNULL_MESSAGE)){
                mobileIsNull=true;
            }
            else if(defaultMessage.equals(ApiValitationConstantMessage.MOBILE_REG_MESSAGE)){
                mobileIsReg=true;
            }else if(defaultMessage.equals(ApiValitationConstantMessage.VCODE_NOTNULL_MESSAGE)){
                codeIsNull=true;
            }
        }
        if(mobileIsNull){
            resultModel.setMessage(ResultStatus.MOBILE_ISNOTNULL.getMessage());
            resultModel.setCode(ResultStatus.MOBILE_ISNOTNULL.getCode());
            return true;
        }
        if(mobileIsReg){
            resultModel.setMessage(ResultStatus.MOBEILE_ERROR.getMessage());
            resultModel.setCode(ResultStatus.MOBEILE_ERROR.getCode());
            return true;
        }
        if(codeIsNull){
            resultModel.setMessage(ResultStatus.CODE_NOTNULL_ERROR.getMessage());
            resultModel.setCode(ResultStatus.CODE_NOTNULL_ERROR.getCode());
            return true;
        }
        return false;
    }
    /***校验手机号码和密码*/
    public boolean ValidMobileAndPassword(BindingResult bindingResult, ResultModel resultModel) {
        boolean userNameIsNull=false;
        boolean passwordIsNull=false;
        List<ObjectError> list = bindingResult.getAllErrors();
        for (ObjectError error : list) {
            String defaultMessage=error.getDefaultMessage();
            if(defaultMessage.equals(ValitationConstantMessage.USERNAME_NOTNULL_MESSAGE)){
                userNameIsNull=true;
            }
            else if(defaultMessage.equals(ValitationConstantMessage.PASSWORD_NOTNULL_MESSAGE )){
                passwordIsNull=true;
            }
        }
        if(userNameIsNull){
            resultModel.setMessage(AdminLoginResultStatus.USERNAME_NOTNULL.getMessage());
            resultModel.setCode(AdminLoginResultStatus.USERNAME_NOTNULL.getCode());
            return true;
        }
        if(passwordIsNull){
            resultModel.setMessage(AdminLoginResultStatus.PASSWORD_NOTNULL.getMessage());
            resultModel.setCode(AdminLoginResultStatus.PASSWORD_NOTNULL.getCode());
            return true;
        }

        return false;
    }
}
